package org.bite.lesson07;

import java.util.concurrent.TimeUnit;

/**
 * 生产消费者模型
 */
public class Demo703 {
    public static void main(String[] args) {
        //阻塞队列作为交易场所
        MyBlockingQueue queue = new MyBlockingQueue(100);
        Thread producer = new Thread(()->{
            int num = 1;
            while(true){
                try{
                    queue.put(num);
                    System.out.println("生产了一个元素 " + num);
                    num++;
                    TimeUnit.MILLISECONDS.sleep(50);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        producer.start();

        Thread consumer = new Thread(()->{
            while(true){
                try{
                    System.out.println("消费元素 " + queue.take());
                    TimeUnit.SECONDS.sleep(1);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        consumer.start();
    }
}
